using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmMemberOperation : Form {
        private FrmUpdateMember ObjFrmUpdateMember = new FrmUpdateMember();
        private MemberBLL _memberBll = MemberBLL.GetMemberBll();
        private int pageSize = 3;
        private int pageNumber = 1;
        private int pageCount = 0;

        public FrmMemberOperation() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmMemberOperation_Load(object sender, EventArgs e) {
            DataTransEvent += ObjFrmUpdateMember.InitMemberInfo;
            comboBox1.Items.AddRange(new Object[] { 3, 5 });
            comboBox1.SelectedIndex = 0;
            UpdateDGV(pageNumber, pageSize);
        }

        private void UpdateDGV(int pageNumber, int pageSize) {
            this.pageNumber = pageNumber;
            this.pageSize = pageSize;
            label2.Text = pageNumber.ToString();
            pageCount = _memberBll.GetPageCount(pageSize);
            label4.Text = pageCount.ToString();
            dgvMemmber.DataSource = _memberBll.PagingQuery(pageNumber, pageSize);
        }


        //关闭时将主窗体维护对象设置为null以便回收器回收
        private void FrmMemberOperation_FormClosing(object sender, FormClosingEventArgs e) {
            FrmMain.ObjFrmMemberOperation = null;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            UpdateDGV(1, int.Parse(comboBox1.SelectedItem.ToString()));
        }

        //上一页
        private void button2_Click(object sender, EventArgs e) {
            if (pageNumber == 1) {
                return;
            }

            UpdateDGV(pageNumber - 1, pageSize);
        }

        //下一页
        private void button3_Click(object sender, EventArgs e) {
            if (pageNumber == pageCount) {
                return;
            }

            UpdateDGV(pageNumber + 1, pageSize);
        }

        //新增会员窗口
        public static FrmAddMember ObjFrmAddMember = null;
        private void btnAddMemMber_Click(object sender, EventArgs e) {
            if (ObjFrmAddMember == null) {
                ObjFrmAddMember = new FrmAddMember();
                ObjFrmAddMember.Show();
            } else {
                ObjFrmAddMember.Activate();
            }
        }

        private event Action<Member> DataTransEvent;
        //修改会员信息窗口
        private void btnUpdateMember_Click(object sender, EventArgs e) {
            if (ObjFrmUpdateMember == null) {
                ObjFrmUpdateMember = new FrmUpdateMember();
                ObjFrmUpdateMember.Closing += (o, s) => { ObjFrmUpdateMember = null; };
            }
            DataGridViewRow dataGridViewRow = dgvMemmber.CurrentRow;
            DataTransEvent.Invoke(new Member() {
                Mid = int.Parse(dataGridViewRow.Cells["mid"].Value.ToString()),
                Mname = dataGridViewRow.Cells["mname"].Value.ToString(),
                Gender = dataGridViewRow.Cells["gender"].Value.ToString(),
                Type = int.Parse(dataGridViewRow.Cells["type"].Value.ToString()),
                Discount = double.Parse(dataGridViewRow.Cells["discount"].Value.ToString()),
                Money = double.Parse(dataGridViewRow.Cells["money"].Value.ToString()),
                Points = int.Parse(dataGridViewRow.Cells["points"].Value.ToString()),
                Birthday = Convert.ToDateTime(dataGridViewRow.Cells["birthday"].Value),
                PhoneNum = dataGridViewRow.Cells["phonenum"].Value.ToString(),
                RegistDate = Convert.ToDateTime(dataGridViewRow.Cells["registdate"].Value),
                EndDate = Convert.ToDateTime(dataGridViewRow.Cells["enddate"].Value)
            });
            ObjFrmUpdateMember.ShowDialog();


            if (ObjFrmUpdateMember.DialogResult == DialogResult.OK) {
                UpdateDGV(pageNumber, pageSize);
            }
        }

        //逻辑删除
        private void btnDelete_Click(object sender, EventArgs e) {
            Result<Member> result = _memberBll.DeleteMember(new Member() {
                Mid = int.Parse(dgvMemmber.CurrentRow.Cells["mid"].Value.ToString())
            }, true);
            UpdateDGV(pageNumber, pageSize);
            MessageBox.Show(result.Message);
        }

        private void button5_Click(object sender, EventArgs e) {
            string queryStr = textBox1.Text;

            if (string.IsNullOrWhiteSpace(queryStr)) {
                UpdateDGV(pageNumber, pageSize);
                return;
            }

            int mid;
            if (int.TryParse(queryStr, out mid)) {
                dgvMemmber.DataSource = _memberBll.QueryByID(mid);
            } else {
                dgvMemmber.DataSource = _memberBll.QueryByName(queryStr);
            }
        }

        public FrmMemberRecycleBin ObjFrmRecycleBin = null;
        //回收站
        private void button1_Click(object sender, EventArgs e) {
            if (ObjFrmRecycleBin == null) {
                ObjFrmRecycleBin = new FrmMemberRecycleBin();
                ObjFrmRecycleBin.Show();
            } else {
                ObjFrmRecycleBin.Activate();
            }
        }
    }
}